use std::io::Write;


pub fn logging_enabled() -> bool {
    return true;
}

pub fn write_log_entry(entry: std::fmt::Arguments) {
    if !logging_enabled() {
        println!("不记录日志");
    }

    let mut log_file = std::fs::OpenOptions::new()
        .create(true)
        .write(true)
        .append(true)
        .open("log.txt")
        .unwrap();
    log_file.write(dt_format().as_bytes()).expect("无法写入日志");
    log_file.write(" ".as_bytes()).expect("无法写入日志");
    log_file.write_fmt(entry).expect("无法写入日志");
    log_file.write("\n".as_bytes()).expect("无法写入日志");
}

macro_rules! log {
    ($format:tt, $($arg:expr),*) => {
        ch_log::write_log_entry(format_args!($format, $($arg),*));
    };
}

fn dt_format() -> String {
    use std::time::{UNIX_EPOCH, SystemTime} ;
    let sys_now = SystemTime::now();

    println!("sys_now = {:?}", sys_now);

    let now =sys_now.duration_since(UNIX_EPOCH).unwrap();
    // let now = Duration:;
    let bb = now.as_secs();
    return dt_format_with_unix_time(bb);
}

pub fn dt_format_with_unix_time(mut bb: u64) -> String {
    let miao = bb % 60;
    bb -= miao;
    let fen = (bb % 3600) / 60;
    bb -= fen * 60;
    let shi = (bb % 86400) / 3600;
    bb -= shi * 3600;
    let mut tian = bb / 86400;
    let mut year = 1970;
    // 根据tian的值计算从1970年1月1日开始的天数
    while tian > 0{
        let mut days = 365;
        if year % 4 == 0 && year % 100 != 0 || year % 400 == 0 {
            days = 366;
        }
        if tian >= days {
            tian -= days;
        } else {
            break;
        }
        year += 1;
    }

    // 计算月份和日期
    let mut month = 1;
    let mut day = 1;
    let days = if year % 4 == 0 && year % 100 != 0 || year % 400 == 0 {366} else {365};
    while tian > 0 {
        let days_of_month = match month {
            1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
            4 | 6 | 9 | 11 => 30,
            2 => if days == 366 {29} else {28},
            _ => 0,
        };
        if tian < days_of_month {
            day += tian;
            break;
        }
        tian -= days_of_month;
        if tian > 0 {
            month += 1;
        } else {
            day += days_of_month
        }
    }
    // let x = |x:u64| if x >= 10 { x.to_string() } else { "0".to_string() + &x.to_string()};

    // return format!("{}-{}-{} {}:{}:{}", year, x(month), x(day), x(shi+8), x(fen), x(miao));
    return  format!("{:04}-{:02}-{:02} {:02}:{:02}:{:02}", year, month, day, shi+8, fen, miao);
}

pub fn unix_time_from_dt(str:&str) -> u64 {
    let mut iter = str.split_whitespace();
    let mut date = iter.next().unwrap().split("-");
    let mut time = iter.next().unwrap().split(":");
    let year = date.next().unwrap().parse::<u64>().unwrap();
    let month = date.next().unwrap().parse::<u64>().unwrap();
    let day = date.next().unwrap().parse::<u64>().unwrap();
    let hour = time.next().unwrap().parse::<u64>().unwrap();
    let minute = time.next().unwrap().parse::<u64>().unwrap();
    let second = time.next().unwrap().parse::<u64>().unwrap();
    let mut tian = (year - 1970) * 365;
    for i in 1970..year {
        if i % 4 == 0 && i % 100 != 0 || i % 400 == 0 {
            tian += 1;
        }
    }
    let days = if year % 4 == 0 && year % 100 != 0 || year % 400 == 0 {366} else {365};
    for i in 1..month {
        let days_of_month = match i {
            1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
            4 | 6 | 9 | 11 => 30,
            2 => if days == 366 {29} else {28},
            _ => 0,
        };
        tian += days_of_month;
    }
    tian += day - 1;
    return tian * 86400 + (hour -8) * 3600 + minute * 60 + second;
}
